//
//  ScreeningServicesViewController.m
//  Logistics
//
//  Created by hy002 on 2018/1/15.
//  Copyright © 2018年 HUYU. All rights reserved.
//

#import "ScreeningServicesViewController.h"
#import "ServeModel.h"

static NSString *cellPath = @"ScreeningCell";
@interface ScreeningServicesViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)NSArray *allServeAry;
@property (nonatomic, strong)UITableView *tableview;
@property (nonatomic, assign)NSInteger allCount;

@end

@implementation ScreeningServicesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _allCount = 0;
    [self creatUI];
    [self requestServeList];
}

- (void)creatUI
{
    self.navigationItem.title = @"服务类型筛选";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = BacksColor;
    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-STA_NAV_HEIGHT-SCREER_HeiX) style:UITableViewStylePlain];
    _tableview.backgroundColor = BacksColor;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableview];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _allServeAry.count+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellPath];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    NSString *titStr = @"全部服务";
    NSString *countStr = s_Integer(_allCount);
    if (indexPath.row != 0)
    {
        if (_allServeAry.count > 0)
        {
            ServeModel *model = _allServeAry[indexPath.row-1];
            titStr = model.name;
            countStr = s_Integer(model.count);
        }
    }
    UILabel *nameLab = [ZCControl createLabelWithFrame:CGRectMake(15*displaywidth, 5*displaywidth, 120*displaywidth, 35*displaywidth) Font:15.0 Text:titStr Color:BlaTitColor];
    if ([titStr isEqualToString:_nameStr])
    {
        nameLab.textColor = YellowColor;
    }
    [cell.contentView addSubview:nameLab];
    
    UILabel *numLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-65*displaywidth, 15*displaywidth, 50*displaywidth, 15*displaywidth) Font:10.0 Text:[NSString stringWithFormat:@"%@",countStr] Color:RGB(166, 166, 166)];
    if ([titStr isEqualToString:_nameStr])
    {
        numLab.textColor = YellowColor;
    }
    numLab.backgroundColor = BacksColor;
    numLab.textAlignment = NSTextAlignmentCenter;
    numLab.layer.cornerRadius = 7.5*displaywidth;
    numLab.layer.masksToBounds = YES;
    [cell.contentView addSubview:numLab];
    UILabel *rightLab = [ZCControl createLabelWithFrame:CGRectMake(SCREEN_WIDTH-25*displaywidth, 13*displaywidth, 10*displaywidth, 15*displaywidth) Font:12.0 Text:@">" Color:RGB(166, 166, 166)];
    rightLab.backgroundColor = [UIColor clearColor];
    if ([titStr isEqualToString:_nameStr])
    {
        rightLab.textColor = YellowColor;
    }
    rightLab.textAlignment = NSTextAlignmentCenter;
    [cell.contentView addSubview:rightLab];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44*displaywidth, SCREEN_WIDTH, 1*displaywidth)];
    lineView.backgroundColor = LinesColor;
    [cell.contentView addSubview:lineView];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = @"全部服务";
    NSString *idStr = @"";
    if (indexPath.row != 0)
    {
        ServeModel *model = _allServeAry[indexPath.row-1];
        name = model.name;
        idStr = s_str(model.ID);
        
    }
    if (self.closeBlock != nil)
    {
        self.closeBlock(name, idStr);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45*displaywidth;
}

- (void)requestServeList
{
    WEAKSELF;
    NSDictionary *dic = @{ @"cityId":[LTools cacheForKey:SERVER_CityiDNew],
                           @"level":@"2"};
    [HYBaseHandler requestWithTheFormUrl:SERVER_CompanyType Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            _allServeAry = [ServeModel mj_objectArrayWithKeyValuesArray:obj[@"data"]];
            dispatch_async(dispatch_get_main_queue(), ^{
                _allCount = 0;
                for (ServeModel *model in _allServeAry)
                {
                    _allCount += model.count;
                }
                [weakSelf.tableview reloadData];
            });
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:weakSelf.view];
        }
    } failed:^(NSError *error) {
       
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
